mui.init();
var nativeWebview, imm, InputMethodManager;
var initNativeObjects = function() {
    if (mui.os.android) {
        var main = plus.android.runtimeMainActivity();
        var Context = plus.android.importClass("android.content.Context");
        InputMethodManager = plus.android.importClass("android.view.inputmethod.InputMethodManager");
        imm = main.getSystemService(Context.INPUT_METHOD_SERVICE);
    } else {
        nativeWebview = plus.webview.currentWebview().nativeInstanceObject();
    }
};
var showSoftInput = function() {
    if (mui.os.android) {
        imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
    } else {
        nativeWebview.plusCallMethod({
            "setKeyboardDisplayRequiresUserAction": false
        });
    }
    setTimeout(function() {
       //此处可写具体逻辑设置获取焦点的input
       var inputElem = document.querySelector('#money');
              inputElem.focus(); 
    }, 200);
};
mui.plusReady(function(){
	initNativeObjects();
	showSoftInput();
	document.getElementById("money").focus();

	var getUrl = apiurl + "wallet/getDefWalletTokens/?user_id=" + getQueryString("user_id");
//	console.log(getUrl);
	mui.getJSON(getUrl, function(res) {
		document.getElementById("tokenslist").innerHTML = template('list-template', {
			"tokens": res.data
		});
	});
	document.getElementById('red_packet_room').addEventListener("tap",function(){
				mui.openWindow({
					url: '../../pages/system/redpacket_help.html',
					id: '../../pages/system/redpacket_help.html'
		});
	});
});


function allottype(type) {
	if (type == 1) {
		document.getElementById('red_type').innerHTML = '单个数量';
	} else {
		document.getElementById('red_type').innerHTML = '总数量';
	}

	cal_money();
}

function getRadioValue(name) {
	var value;
	var radios = document.getElementsByName(name);
	for (var i = 0; i < radios.length; i++) {
		if (radios[i].checked) {
			value = radios[i].value;
			break;
		}
	}
	return value;
}

function cal_money(obj) {
	try {	
		document.getElementById('all_money').innerHTML = 0.000;
		document.getElementById('all_money1').innerHTML = 0.000;
		mui('.mui-btn-danger.mui-btn-block')[0].style.opacity = 0.3;

		var allottype = getRadioValue("allottype");
		var money = parseFloat(document.getElementById("money").value);
		var count = parseInt(document.getElementById("count").value);
		var total = money;
		if(isNaN(money) || isNaN(count))
		{
			return 0.000;
		}

		if(count != document.getElementById("count").value){
			alert("红包数量必须为整数");
			return 0.000;
		}

		if (allottype == 1) {
//			total =  Math.round(parseFloat(money * count)*100)/100;
			total =  money * count;
			if (money < 0.001) {
				alert("最小个数为0.001");
				return 0;
			}
		} else {
			if ((money / count) < 0.001) {
				alert("最小个数为0.001");
				return 0;
			}
		}

		document.getElementById('all_money').innerHTML = total;
		document.getElementById('all_money1').innerHTML = total;
		mui('.mui-btn-danger.mui-btn-block')[0].style.opacity = 1;
		return total;
	} catch (e) {

	}
	return 0;
}

function prepSend() {
	if (cal_money() > 0) {
		document.getElementById('sendTransMain').style.display = "none";
		document.getElementById('popupConfirmSend').style.display = "block";
	} else {
		alert("请输入合理的数量");
		return;
	}
}

function cancel() {
	document.getElementById('popupConfirmSend').style.display = "none";
	document.getElementById('sendTransMain').style.display = "block";
}

function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]);
	return "";
}

function SubmitRedPacket() {
	var count = parseFloat(document.getElementById("count").value);
	var money = cal_money();
	var key = document.getElementById("password").value;
	if (money < 0.001) {
		alert("请输入合理的数量");
		return;
	}
	if (count < 1) {
		alert("请输入红包数量");
		return;
	}
	if(key.length < 1)
	{
		alert("请输入密码");
		return;
	}

	var allottype = getRadioValue("allottype");
	var symbol = document.getElementById("tokenslist").value;//getRadioValue("symbol");
	var remark = document.getElementById("remark").value;
	var postUrl = apiurl + "wallet/addTrade";
	remark = remark == "" ? "恭喜发财，大吉大利" : remark;

	var postdata = {
		wallet_id: getQueryString("id"),
		user_id: getQueryString("user_id"),
		roomid: getQueryString("roomid"),
		from:"",
		count:count,
		fromtype: 4,
		allottype: allottype,
		remark: remark,
		key: key,
		symbol: symbol,
		money: money,
	};
	//im.playsendSound('hb');
	mui.post(postUrl, postdata, function(res) {
		if(res.status == 1)
		{
			var vPrePage = plus.webview.getWebviewById('chatbox');
			if(vPrePage != null)
			{
				console.log("qunhongbao");
				//触发下一个页面的自定义事件
				mui.fire(vPrePage, 'PAY_ROOM_HONGBAO_SUCC', {
					detailid: res.data.detailid,
					tradeid: res.data.tradeid,
					remark: remark,
					money: money + symbol
				});
			}
			mui.alert(res.msg);
			mui.back();
		}
		else{
			mui.alert(res.msg);
		}
	});
	
}